<?php

//模板相关函数

use app\bll\Site as BllSite;
use core\lib\paginator\FcPaginator;
use core\tpl\PhpTpl;
use Shopwwi\LaravelCache\Cache;

if (!function_exists('fc')) {
    function fc(string $app, ...$params)
    {

        $apps = explode(':', $app);
        $appname = 'frame';
        $bllname = '';
        $method = '';
        if (count($apps) == 2) {
            $bllname =  trim($apps[0]);
            $method = trim($apps[1]);
        }

        if (count($apps) == 3) {
            $appname = trim($apps[0]);
            $bllname = trim($apps[1]);
            $method = trim($apps[2]);
        }

        if ($appname == '' || $bllname == '' || $method == '') {
            throw new Exception('模板标签缺少参数');
        }
        return PhpTpl::$appname($bllname, $method, ...$params);
    }
}


if (!function_exists('now_site')) {
    /**
     * 获取当前站点
     *
     * @return array
     */
    function now_site(): array
    {

        $host = request()->host();

        $nowsiteinfo = Cache::get('nsiteinfo_' .  $host);
        if (!$nowsiteinfo) {
            $bllSite = new BllSite();
            $siteinfo = $bllSite->findByDomain($host);
            Cache::add('siteinfo_' .  $host, $siteinfo);
            return $siteinfo;
        }

        return $nowsiteinfo;
    }
}

if (!function_exists('paginator')) {
    /**
     * 分页
     *
     * @param integer $total
     * @param integer $pagesize
     * @param string $url
     * @return string
     */
    function paginator(int $total, int $pagesize, string $url = ''): string
    {
        $RcPaginator = new FcPaginator(
            total: $total,
            pagesize: $pagesize,
            url: $url
        );
        return $RcPaginator
            ->getPaginator()
            ->toHtml();
    }
}

if (!function_exists('is_home_page')) {
    /**
     * 判断是不是首页
     *
     * @return boolean
     */
    function is_home_page(): bool
    {
        return get_controller() == 'home/Index' && get_action() == 'index' && get_plugin() == '';
    }
}


if (!function_exists('isFrontPage')) {
    /**
     * 判断是不是后台
     *
     * @return boolean
     */
    function isFrontPage(): bool
    {
        return strncmp(get_controller(), 'home/', 5) === 0;
    }
}

if (!function_exists('isAdminBackend')) {
    /**
     * 判断是不是后台
     *
     * @return boolean
     */
    function isAdminBackend(): bool
    {
        return strncmp(get_controller(), 'admin/', 5) === 0;
    }
}
